set(COPIEXE_SOURCE_FILES
  lua_docall.h lua_docall.c
  copi.h main.c winfile.c)

if (WIN32)
  set(ICON copi.rc)
endif ()

set(luasocket_library_path "${Z_VCPKG_ROOT_DIR}/packages/luasocket_${VCPKG_TARGET_TRIPLET}")
set(luafilesystem_library_path "${Z_VCPKG_ROOT_DIR}/packages/luafilesystem_${VCPKG_TARGET_TRIPLET}")

find_package(lua REQUIRED)
# find_package(socket.core REQUIRED)

add_executable(COPIEXE WIN32 ${ICON} ${COPIEXE_SOURCE_FILES})

# add_executable(COPIEXE ${ICON} ${COPIEXE_SOURCE_FILES})

target_include_directories(COPIEXE
  PRIVATE ${CMAKE_SOURCE_DIR}/logc/include
  PRIVATE ${CMAKE_SOURCE_DIR}/ui/include
  INTERFACE ${LUA_INCLUDE_DIR})

target_link_libraries(COPIEXE
  PUBLIC ${LUA_LIBRARIES}
  PRIVATE UI
  PRIVATE logc)

if (MSVC)
  set_target_properties(COPIEXE PROPERTIES LINK_FLAGS "/ENTRY:mainCRTStartup")
elseif (CMAKE_C_SIMULATE_ID STREQUAL "MSVC")
  set_target_properties(COPIEXE PROPERTIES LINK_FLAGS "-Wl,/entry:mainCRTStartup")
endif ()

set_target_properties(COPIEXE PROPERTIES
  OUTPUT_NAME "copi"
  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/dist"
  C_STANDARD 99)

target_compile_definitions(COPIEXE PRIVATE UNICODE _UNICODE)

set(COPYLUACMD
  COMMAND ${CMAKE_COMMAND} -E copy_directory
  ${CMAKE_SOURCE_DIR}/lua
  ${CMAKE_SOURCE_DIR}/dist/lua)

add_custom_target(copylua)
add_custom_command(TARGET copylua POST_BUILD
  ${COPYLUACMD})

add_custom_command(TARGET COPIEXE POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy
  ${CMAKE_SOURCE_DIR}/3rd/irrklang/irrKlang.dll
  ${CMAKE_SOURCE_DIR}/dist/irrKlang.dll

  COMMAND ${CMAKE_COMMAND} -E copy
  ${CMAKE_SOURCE_DIR}/3rd/irrklang/ikpFlac.dll
  ${CMAKE_SOURCE_DIR}/dist/ikpFlac.dll

  COMMAND ${CMAKE_COMMAND} -E copy
  ${CMAKE_SOURCE_DIR}/3rd/irrklang/ikpMP3.dll
  ${CMAKE_SOURCE_DIR}/dist/ikpMP3.dll

  COMMAND ${CMAKE_COMMAND} -E copy
  ${luasocket_library_path}/bin/mime/core.dll
  ${CMAKE_SOURCE_DIR}/dist/clib/mime/core.dll

  ${COPYLUACMD}

  COMMAND ${CMAKE_COMMAND} -E copy
  ${luasocket_library_path}/bin/socket/core.dll
  ${CMAKE_SOURCE_DIR}/dist/clib/socket/core.dll

  COMMAND ${CMAKE_COMMAND} -E copy_directory
  ${CMAKE_SOURCE_DIR}/assets/audio
  ${CMAKE_SOURCE_DIR}/dist/audio

  COMMAND ${CMAKE_COMMAND} -E copy
  ${CMAKE_SOURCE_DIR}/fonts/harmony.ttf
  ${CMAKE_SOURCE_DIR}/dist/fonts/harmony.ttf
  )
